var readline = require('readline');
var demoget = require('./demo');
var config = require('config');

var express=require('express');
var app =express();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

var rl = readline.createInterface(process.stdin, process.stdout);

var recursiveAsyncReadLine = function (answer,num,interval) {
   /* rl.question('Command: ', function (answer) { */
       switch (answer){
           case 'exit':
             return rl.close();
           case 'g1':
           case 'g2':
           case 'g3':
           case 'g4':
           case 'g5':
           case 'g6':
           case 'g7':
           case 'g8':
           case 'g9':
		   return new Promise(function (resolve, reject) {
				demoget.demoget(answer,num,interval).then(data=>{
					resolve(data);
			   });
		   })
			break;
           case 'p1':
           case 'p2':
           case 'p3':
           case 'p4':
           case 'p5':
           case 'p6':
           case 'p7':
           case 'p8':
           case 'p9':
           case 'p10':
			
		   return new Promise(function (resolve, reject) {
			    demoget.demopost(answer).then(data=>{
					resolve(data);
			   });
		   })
           break;
           case 'h':
             console.log('Get指令列表如下:');
             var getIntfs = config.interfaces.get_methods;
             getIntfs.forEach(e => {
               console.log(e.tip, e.intf_no);
             });
             console.log('Post指令列表如下:');
             var postIntfs = config.interfaces.post_methods;
             postIntfs.forEach(e => {
               console.log(e.tip, e.intf_no);
             });
             break;  
   
           default:
             console.log('请输入指令, 比如g1, g2, p1, p2..., 指令列表请输入h, 退出输入exit');break;  
               
       }
    // recursiveAsyncReadLine(); //Calling this function again to ask new question
  /* }); */
  };

var questions=[
    {
        data:213,
        num:444,
        age:12
    },
    {
        data:456,
        num:678,
        age:13
    }];
app.get('/',function(req,res){
	console.log(req.query)
    res.status(200)
	
		recursiveAsyncReadLine(req.query.code,req.query.num,req.query.time).then(data=>{
            var nowMean = 0;
            var nowBefore = 0;
            var now=data.slice(0,req.query.num-10);
            var before=data.slice(10,req.query.num);
            now.forEach(x=>{
                nowMean+=x.close
            });
            before.forEach(x=>{
                nowBefore+=x.close
            });
            var obj={};
            obj.now= Math.floor(nowMean/(req.query.num-10)*10000)/10000;
            obj.before=Math.floor(nowBefore/(req.query.num-10)*10000)/10000;
			res.json(
			  obj
			)
		})

	
	 /* res.json(recursiveAsyncReadLine(req.query.code)) */
});
//配置服务端口
var server = app.listen(3000, function () {

    var host = server.address().address;

    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
})
